package com.hspedu.furns.dao.impl;

import com.hspedu.furns.dao.BasicDAO;
import com.hspedu.furns.dao.FurnDAO;
import com.hspedu.furns.entity.Furn;
import com.hspedu.furns.entity.Page;

import java.util.List;

/**
 * @author 徐
 * @version 1.0
 */
public class FurnDAOImpl extends BasicDAO<Furn> implements FurnDAO {

    /**
     * 返回家居集合
     * @return
     */
    @Override
    public List<Furn> queryFurns() {
        String sql = "SELECT `id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path` imgPath\n" +
                "FROM furn";
        return queryMulti(sql,Furn.class);
    }

    /**
     * 保存家居信息,返回执行成功的条数
     * @param furn
     * @return
     */
    @Override
    public int addFurn(Furn furn) {
        String sql = "INSERT INTO furn(`id` , `name` , `maker` , `price` , `sales` , `stock` , `img_path`) \n" +
                "VALUES(NULL, ? , ? , ? , ? , ? , ?);";
        return update(sql,furn.getName(),furn.getMaker(),
                furn.getPrice(),furn.getSales(),furn.getStock(),furn.getImgPath());
    }

    /**
     * 删除家居信息
     * @param id
     */
    @Override
    public int deleteFurnById(Integer id) {
        String sql = "DELETE FROM furn WHERE id = ?";
        return update(sql, id);
    }

    /**
     * 根据id查询家居信息
     * @param id
     * @return
     */
    @Override
    public Furn queryFurnById(Integer id) {
        String sql = "SELECT `id` , `name` , `maker` , `price` , `sales` ," +
                " `stock` , `img_path` imgPath FROM furn WHERE id = ?";
        return querySingle(sql,Furn.class,id);
    }

    /**
     * 根据传入的furn对象id
     * 修改家居对象信息
     * @param furn
     * @return
     */
    @Override
    public int updateFurn(Furn furn) {
        String sql = "UPDATE furn \n" +
                "SET `name` = ?,\n" +
                "`maker` = ?,\n" +
                "`price` = ?,\n" +
                "`sales` = ?,\n" +
                "`stock` = ?,\n" +
                "`img_path` = ?\n" +
                "WHERE `id` = ?;";
        return update(sql, furn.getName(), furn.getMaker(),
                furn.getPrice(), furn.getSales(), furn.getStock(),
                furn.getImgPath(), furn.getId());
    }

    /**
     * 查询家居信息总数
     * @return
     */
    @Override
    public int getTotalRow() {
        String sql = "SELECT COUNT(*) FROM furn";
        //(Interage)queryScalar(sql) 发生异常
        //long cannot be cast to java.lang.Integer
        return ((Number)queryScalar(sql)).intValue();
    }

    /**
     * 返回当前页码的家居信息
     * @return
     */
    @Override
    public List<Furn> getPageItems(int begin, int pageSize) {
        String sql = "SELECT `id` , `name` , `maker` , `price` , `sales` ," +
                " `stock` , `img_path` imgPath FROM furn LIMIT ?,?";
        return queryMulti(sql,Furn.class,begin,pageSize);
    }

    /**
     * 根据传入的名字，返回符合的家居信息数
     * @param name
     * @return
     */
    @Override
    public int getTotalRowByName(String name) {
        String sql = "SELECT COUNT(*) FROM furn WHERE `name` LIKE ?";
        return  ((Number)queryScalar(sql,"%" + name + "%")).intValue();
    }

    /**
     * 根据begin, pageSize, 和name来获取要显示的家居信息
     * @param begin
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public List<Furn> getPageItemsByName(int begin, int pageSize, String name) {
        String sql = "SELECT `id`, `name` , `maker`, `price`, `sales`, `stock`, " +
                "`img_path` imgPath FROM furn WHERE `name` LIKE ? LIMIT ?, ?";
        return queryMulti(sql,Furn.class, "%" + name + "%", begin, pageSize);
    }

    /**
     * 根据id返回家居的图片路径
     * 用于当修改图片时，删除老的家居图片信息
     * @param id
     * @return
     */
    @Override
    public String getFurnImgPath(Integer id) {
        String sql = "SELECT `img_path` \n" +
                "FROM furn\n" +
                "WHERE `id` = ?";
        return (String) queryScalar(sql,id);
    }
}
